package com.hbue.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.hbue.bean.Book;
import com.hbue.service.BookService;
import com.hbue.service.impl.BookServiceImpl;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@WebServlet("/book")
public class BookController extends HttpServlet {

    private BookService bookService = new BookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理乱码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset= utf-8");
        String method = req.getParameter("method");
        if (method != null && method.equals("findAllBook")) {
            findAllBook(req, resp);
        } else if (method != null && method.equals("deleteBook")) {
            deleteBook(req, resp);
        } else if (method != null && method.equals("findBook")) {
            findBook(req, resp);
        } else if (method != null && method.equals("update")) {
            try {
                updateBook(req, resp);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        } else if (method != null && method.equals("addBook")) {
            try {
                addBook(req,resp);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if (method != null && method.equals("search")){
            findBookBySearchContent(req,resp);
        }
    }

    /**
     * 根据搜索条件搜索书籍信息
     * @param req
     * @param resp
     */
    private void findBookBySearchContent(HttpServletRequest req, HttpServletResponse resp) {
        String searchContent = req.getParameter("searchContent");
        ArrayList<Book> books= bookService.findBookBySearchContent(searchContent);
        ServletContext servletContext = getServletContext();
        servletContext.setAttribute("books",books);
    }

    /**
     * 添加数据信息
     * @param req
     * @param resp
     */
    private void addBook(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        try {
                String bookName = req.getParameter("bookName");
                String author = req.getParameter("author");
                String publisher = req.getParameter("publisher");
                String date = req.getParameter("date");
                String price = req.getParameter("price");
                String usage = req.getParameter("usage");
                String isbn = req.getParameter("ISBN");
                Book book = new Book(null, bookName, author, publisher, new SimpleDateFormat("yyyy-MM-dd").parse(date), Double.parseDouble(price), usage, isbn);
                bookService.addBook(book);
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().write("添加失败");
        }

    }

    /**
     * 修改图书信息
     *
     * @param req
     * @param resp
     */
    private void updateBook(HttpServletRequest req, HttpServletResponse resp) throws ParseException {
        try {
            String id = req.getParameter("id");
            String bookName = req.getParameter("bookName");
            String author = req.getParameter("author");
            String publisher = req.getParameter("publisher");
            String date = req.getParameter("date");
            Date date1 = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            String price = req.getParameter("price");
            String usage = req.getParameter("usage");
            String isbn = req.getParameter("ISBN");
            Book book = new Book(Integer.parseInt(id), bookName, author, publisher, date1, Double.parseDouble(price), usage, isbn);
            bookService.updateBook(book);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询单本书籍的信息
     *
     * @param req
     * @param resp
     */
    private void findBook(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        Book book = bookService.findBook(id);
        ObjectMapper objectMapper = new ObjectMapper();
        String JSON = objectMapper.writeValueAsString(book);
        resp.getWriter().write(JSON);
    }

    /**
     * 删除图书
     *
     * @param req
     * @param resp
     */
    private void deleteBook(HttpServletRequest req, HttpServletResponse resp) {
        try {
            String id = req.getParameter("id");
            bookService.deleteBook(id);
            resp.getWriter().write("删除成功!");
        } catch (Exception e) {
            e.printStackTrace();
            try {
                resp.getWriter().write("删除失败!");
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    /**
     * 查询所有图书
     *
     * @param req
     * @param resp
     */
    private void findAllBook(HttpServletRequest req, HttpServletResponse resp) {
        ArrayList<Book> books = bookService.findAllBook();
        ServletContext context = getServletContext();
        context.setAttribute("books", books);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
